package com.project.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.SupplierBean;
import com.project.bean.UserBean;
import com.project.service.ISupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("supplier")
public class SupplierController {
    @Autowired
    private ISupplierService service;
@RequestMapping("findByItem")
    public Page<SupplierBean> findByItem(Integer pageNo, String name, String ranges){
        return service.findByItem(pageNo,name,ranges);
    }
@RequestMapping("findById")
    public SupplierBean findById(Integer id){
    return service.findById(id);
    }
@RequestMapping("update")
    public String update(Integer id,String cadre,String phone){
    service.update(id,cadre,phone);
    return "ok";
    }
@RequestMapping("add")
    public String  add(String name, String cadre, String phone, String address, String ranges){
      SupplierBean supplier = new SupplierBean(name,cadre,phone,address,ranges);
     service.add(supplier);
     return "ok";
    }
@RequestMapping("findAll")
    public List<SupplierBean> findAll(){
    return service.findAll();
    }

    @RequestMapping("addFile")
    public String  addFile(@RequestParam("excelFile") MultipartFile mf) throws Exception {
        //定义导入设置
        ImportParams ims = new ImportParams();
        //设置标题栏占几行
        ims.setTitleRows(1);
        //设置表头占几行
        ims.setHeadRows(1);
        //设置读取第几个选项卡内容
        ims.setSheetNum(1);
        //将上传文件流，转换为集合，第一个参数为上传文件读取流，第二个参数为集合存放元素类模板
        //第三个参数为导入设置对象
        List<SupplierBean> list =  ExcelImportUtil.importExcel(mf.getInputStream(),SupplierBean.class,ims);
        service.addList(list);
        return "ok";
    }
    }

